/* Copyright 2024 Coremail
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// import {}
import { ErrorCode } from '@coremail/mail_base'

export class ErrorUtil {
  static codeToMsg(code: number): string {
    switch (code) {
      case ErrorCode.Error:
      case ErrorCode.UNKNOWN_ERROR:
        return "未知异常";
      case ErrorCode.CONNECT_TIMEOUT:
        return "连接服务器超时";
      case ErrorCode.LOGIN_FAILED:
        return "账号或密码错误";
      case ErrorCode.CONNECT_FAILED:
        return "连接服务器失败";
      case ErrorCode.PARAMETER_ERROR:
        return "参数错误";
      case ErrorCode.MAIL_NOT_FOUND:
        return "邮件不存在";
      case ErrorCode.FOLDER_NOT_FOUND:
      case ErrorCode.FOLDER_EXISTS:
        return "邮件文件夹不存在";
      case ErrorCode.PARSE_MAIL_FAILED:
        return "解析邮件内容出错";
      case ErrorCode.NO_AVAILABLE_NETWORK:
        return "网络不可用";
      case ErrorCode.SERVER_UN_REACHABLE:
      case ErrorCode.SERVER_NOT_SUPPORT:
        return "服务器连接异常";
      case ErrorCode.FOLDER_NOT_FOUND:
        return "邮件文件夹不存在";
      case ErrorCode.CREATE_FOLDER_FAILED:
        return "创建文件夹失败";
      case ErrorCode.MAIL_NOT_FOUND:
        return "邮件不存在";
      case ErrorCode.MAIL_PART_NOT_FOUND:
        return "解析邮件失败";
      case ErrorCode.MAIL_ATTRIBUTE_NOT_FOUND:
      case ErrorCode.MAIL_ATTRIBUTE_NOT_SUPPORT:
      case ErrorCode.MAIL_ATTRIBUTE_NOT_WRITABLE:
        return "未知异常";
      case ErrorCode.SENT_MAIL_FAILED:
      case ErrorCode.PROTOCOL_ERROR:
        return "发信失败";
      default:
        return "未知异常";
    }
  }

  static getCodeErr(code: number, protocol: string): string {
    switch (code) {
      case ErrorCode.LOGIN_FAILED:
        return "账号或密码错误，请确认信息填写是否正确。";
      case ErrorCode.CONNECT_FAILED:
      case ErrorCode.CONNECT_TIMEOUT:
      case ErrorCode.UNKNOWN_ERROR:
        return `无法连接到${protocol.toUpperCase()}服务器，可能服务器或端口填写错误，请检查后重试。`;
      case ErrorCode.NO_AVAILABLE_NETWORK:
      default:
        return "网络异常，请检查网络后重试。";
    }
  }
}